package br.ucb.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.ucb.beans.ADM;
import br.ucb.connection.FactoryConnection;
import br.ucb.servicos.EncriptaSenha;

public class AdmDAO {
	private static final String ACESSO = "G";
	private static final String STATUS = "A";
	private static final String TIPO = "F";
	public static int addTrabalhador(ADM adm) throws SQLException, ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PessoaDAO.addPessoa(new ADM(adm.getNome(),adm.getSexo()));

		String SQL_INSERT = "INSERT INTO daoTrabalhador(chavePessoa, cpf,acesso, status,tipo,dtNascer) VALUES (?,?,'"+ACESSO+"','"+STATUS+"','"+TIPO+"',?)";
		PreparedStatement instrucao = con.prepareStatement(SQL_INSERT);

		String sqlLastId = "SELECT MAX(chavePessoa) id FROM daoPessoa";
		PreparedStatement instrucao2 = con.prepareStatement(sqlLastId);
		ResultSet rs = instrucao2.executeQuery(sqlLastId);

		if(rs.first()){
			instrucao.setInt(1, rs.getInt("id"));
			instrucao.setString(2, adm.getCpf());
			instrucao.setDate(3,Date.valueOf(adm.getData()));
		}
		else
			return 0;
		int retorno = instrucao.executeUpdate();
		String senha = EncriptaSenha.encripta(adm.getSenha());
		AcessoDAO.addAcesso(adm.getCpf(),senha);
		instrucao.close();
		con.close();
		return retorno;
	}
}
